import cn.edu.zut.cs.Business.bookService;
import cn.edu.zut.cs.Model.book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

@WebServlet(name="addbookServlet",value="/addbookServlet")
public class addbookServlet extends HttpServlet {
    book book;
    bookService bookService;

    public void init(ServletConfig config) throws ServletException{
        super.init(config);
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,config.getServletContext());
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
        PrintWriter out=response.getWriter();

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ISDN=request.getParameter("ISDN");
        String name = request.getParameter("bname");
        String author = request.getParameter("author");
        float price = Float.parseFloat(request.getParameter("price"));
        if (ISDN.equals("") || ISDN.equals(null) ) {
            JOptionPane.showMessageDialog(null, "输入的ISDN不能为空", "标题【警告】", JOptionPane.ERROR_MESSAGE);
            response.sendRedirect("addbook.html");
            return;
        }else{
            book.setIsdn(ISDN);
            book.setBname(name);
            book.setAuthor(author);
            book.setPrice(price);
            bookService.insert_book(book);
            PrintWriter out=response.getWriter();
            out.println(ISDN);
            out.println(name);
            out.println(author);
            out.println(price);
            response.sendRedirect("addbook.html");
        }

    }
    @Autowired
    public book getBook() {
        return book;
    }
    @Autowired
    public void setBook(book book) {
        this.book = book;
    }
    @Autowired
    public bookService getBookService() {
        return bookService;
    }
    @Autowired
    public void setBookService(bookService bookService) {
        this.bookService = bookService;
    }
}